<?php
/**
 * 描述
 * Created by Paco
 * Date: 2018/8/17 Time: 11:30
 */

/**
 * 生成uuid
 */
if (!function_exists('uuid')) {
    function uuid()
    {
        $charId = md5(uniqid(rand(), true));
        $uuid = substr($charId, 0, 8)
            . substr($charId, 8, 4)
            . substr($charId, 12, 4)
            . substr($charId, 16, 4)
            . substr($charId, 20, 12);
        return $uuid;
    }
}

if (!function_exists('check_code')) {
    function check_code($uuid, $code)
    {
        $captcha = 'captcha_' . $uuid;
        if (!app('phpredis')->exists($captcha)) {
            return false;
        }
        $key = app('phpredis')->get($captcha);
        $value = strtolower($code);
        $key = strtolower($key);
        if ($value == $key) {
            app('phpredis')->del($captcha);
            return true;
        } else {
            return false;
        }
    }
}

/**
 * 写入日志
 */
if (!function_exists('log_done')) {
    function log_done($admin_id, $admin_name, $desc, $ip, $content = '')
    {

        $data = [
            'admin_id' => $admin_id,
            'admin_name' => $admin_name,
            'desc' => $desc,
            'content' => $content,
            'ip' => $ip,
        ];
        \App\Models\Log\Log::query()->create($data);
    }
}


/**
 * 创建目录
 */
if (!function_exists('my_mkdir')) {
    function my_mkdir($path)
    {
        if (!file_exists($path)) {
            $path = explode("/", $path);
            $real_path = '/';
            foreach ($path as $key => $v) {
                $real_path .= $v;
                if (!file_exists($real_path)) {
                    mkdir($real_path);
                }
            }
        }
    }
}

if (!function_exists('upload')) {
    function upload(\Illuminate\Http\Request $request)
    {
        $file = $request->file('logo');
        $logo = '';
        if ($file) {
            $path = 'storage/uploads/' . date('Ym');
            $extension = $file->guessExtension();
            $newName = uniqid() . "." . $extension;
            $file->move($path, $newName);
            $logo = $path . '/' . $newName;
        }
        return $logo;
    }
}

if (!function_exists('get_config')) {
    function get_config($key, $prefix, $default = '')
    {
        $key_arr = explode('.', $key);
        $key = 'config_' . $prefix . '_' . $key_arr[0];
        $config = json_decode(app('phpredis')->get($key));
        if (!$config) {
            $config = json_decode(\App\Models\System\Config::query()->where('name', $key_arr[0])
                ->where('prefix', $prefix)
                ->value('value'));
            if ($config) {
                app('phpredis')->setex($key, 86400, json_encode($config));
            }
        }

        $tem = [];
        array_shift($key_arr);
        if ($key_arr) {
            foreach ($key_arr as $v) {
                foreach ($config as $i) {
                    if ($v == $i->key) {
                        $tem = $i->value;
                    }
                }
            }
        } else {
            foreach ($config as $k => $v) {
                $tem[$v->key] = $v->value;
            }
            $tem = json_decode(json_encode($tem));
        }
        if (!$tem) {
            $tem = $default;
        }
        return $tem;
    }
}


/**
 * 获取随机字符串
 */
if (!function_exists('random_keys')) {
    function random_keys($length = 8)
    {
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyz   
               ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        $key = '';
        for ($i = 0; $i < $length; $i++) {
            $key .= $pattern{mt_rand(0, 35)};    //生成php随机数
        }
        return $key;
    }
}


if (!function_exists('filter_words')) {
    function filter_words($str)
    {
        if (empty($str)) return '';
        $words = get_config('filter_words.words', 'admin', '');
        $words = explode('|', $words);
        return str_replace($words, '', $str);
    }
}

if (!function_exists('filter_url')) {
    function filter_url($str)
    {
        if (empty($str)) return '';
        return preg_replace('@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@', '', $str);
    }
}


if (!function_exists('my_array_filter')) {
    function my_array_filter($var)
    {
        if ($var === '' ) {
            return false;
        }
        return true;
    }
}


if (!function_exists('fixed_id')) {
    function fixed_id($id, $decode = true)
    {
        if (!$id) {
            return 0;
        }
        if ($decode) {
            $id = 1865943 + $id * 7 - 138;
        } else {
            $id = ($id - 1865943 + 138) / 7;
        }
        return $id;
    }
}